package com.xiyili.topspecialty;

import java.net.URLEncoder;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xiyili.topspecialty.data.Specialty;
import com.xiyili.topspecialty.data.SpecialtyDatabaseHelper;


/**
 * A fragment representing a single Specialty detail screen. This fragment is
 * either contained in a {@link SpecialtyListActivity} in two-pane mode (on
 * tablets) or a {@link TopSchoolListActivity} on handsets.
 */
public class TopSchoolListFragment extends ListFragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String EXTRA_SPECIALTY_CODE = "specialty_code";
	public static final String EXTRA_SPECIALTY_NAME = "specialty_name";
	private String mSpecialtyCode;
	private String mSpecialtyName;



	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public TopSchoolListFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mSpecialtyCode = getArguments().getString(
				EXTRA_SPECIALTY_CODE);
		mSpecialtyName = getArguments().getString(EXTRA_SPECIALTY_NAME);

		List<Specialty> items =  SpecialtyDatabaseHelper.getInstance(getActivity())
				.findSpecialtyByCode(mSpecialtyCode);
		setListAdapter(new SpecialtyAdapter(getActivity(), items));
	}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Specialty sp = (Specialty) l.getAdapter().getItem(position);
        String school_name = URLEncoder.encode(sp.schoolName);
        String bakeUrl = "http://wapbaike.baidu.com/search?word=" + school_name;
        Intent intent = new Intent(getActivity(), BaseWebappActivity.class);
        intent.putExtra("res_url", bakeUrl);
        startActivity(intent);
    }


    static class SpecialtyAdapter extends BaseAdapter {
        private final LayoutInflater mInflater;
		private final List<Specialty> mData;
		public SpecialtyAdapter(Context context,List<Specialty> data) {
			mInflater = LayoutInflater.from(context);
			mData = data;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.specialty_top_school_list_item, parent, false);
				holder  = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.school_name);
				holder.score = (TextView) convertView.findViewById(R.id.score);

				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}

			Specialty item = (Specialty) getItem(position);

			holder.name.setText(item.schoolName);
			holder.score.setText(""+item.score);


			return convertView;
		}

		static class ViewHolder{
			public TextView name;
			public TextView score;
		}



	}


}
